package com.kf.kafka;

import com.kf.pojo.Notice;
import com.kf.service.NoticeRepository;
import com.kf.service.NoticeService;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;

/***
 * @author wjl
 * @desc kafka的消费者
 **/
@Configuration

public class ConsumerKafka {
    @Resource
    NoticeService noticeService;
    @Resource
    NoticeRepository noticeRepository;

    public  String[] getInfo(String msg){
        String[] split =new String[2];
        String[] split1 = msg.split("公告id*");
//:113,预览时间:1000
        String[] split2 = split1[1].split(",");

        split[0]=split2[0].substring(2);
        String[] split3 = split2[1].split(":");
        split[1]=split3[1];
        return split;
    }


    @KafkaListener(topics = "test2")
    public void consumerKafkaTopic(String msg){
        //公告id*:113,预览时间:1000
        boolean contains = msg.contains("公告id*:");
        if(contains){
            String[] info = this.getInfo(msg);
            int id = Integer.parseInt(info[0]);
            System.out.println("====="+id);
            int lookTime = Integer.parseInt(info[1]);
            Notice notice = noticeService.getById(id);
            if(notice!=null){
                long time1 = notice.getGmtModified().getTime();
                double heattime = (time1 % 1000000000L) / 1000.0;
                double heat = (heattime / 2 + lookTime * 100) / 10000;
                double heat1 = notice.getHeat();
                System.out.println("====="+heat1);
                notice.setHeat(heat+heat1);
                noticeService.updateById(notice);
                noticeRepository.save(notice);
            }


        }


    }

}
